# Alexander F. Gazmararian
# afg2@princeton.edu

# Load packages
library(tidyverse)
library(modelsummary)

# Load data
g <- readRDS("data/FairGazmararian_2022.rds")

# Make income ordered
g$income5 <- gsub("[$]", "", g$income5)
g$income5 <- factor(g$income5, ordered = TRUE, levels = c("Less than 29,999", "30,000 - 59,999", "60,000 - 99,999", "100,000 or more", "Prefer not to say"))

# Get summary statistics
file <- "tables/tbl_summarystat_fair2.txt"
datasummary(
  formula = (Age = age) + (Female = Gender)  + (`Non-White` = nonwhite) + (College = college) + (`Fossil Fuel Employment` = ff) + 
    (`Full-Time Employed` = fullemploy) + (Democrat = Dem) + (Republican = Rep) + (Liberal = Lib) + (Conservative = Con) +
    (Income = income5) ~ Mean + SD + N + Percent(),
  data = g,
  fmt = 2,
  escape = FALSE,
  output = "latex"
) %>%
  cat(., file = file)
# Trim table shell
tbl <- readLines(file, warn = FALSE)
tbl <- tbl[-c(1,2,24)]
cat(tbl, file = file)